// 引入axios
import axios from 'axios';
import nProgress from 'nprogress';
import 'nprogress/nprogress.css';

// 创建axios实例
const mockAjax = axios.create({
  baseURL:'/mock',
  timeout:1000
});

// 设置请求拦截器
mockAjax.interceptors.request.use(config => {
  //进度条的开始
  nProgress.start();
  return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 设置响应拦截器
mockAjax.interceptors.response.use(
  response => {
    //进度条的结束
    nProgress.done();
    return response.data;
  },
  error => {
    // 进度条的结束
    nProgress.done();
    return Promise.reject(new Error(error.message))
  }
);

export default mockAjax;